package com.llkj.qingnianguoxue.circle;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.ReportPicsAdapter;
import com.llkj.qingnianguoxue.adapter.ReportReasonAdapter;
import com.llkj.qingnianguoxue.album.AlbumActivity;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.ReportReasonBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.FileProvider7;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.PicCameraLocalUtil;
import com.llkj.qingnianguoxue.util.SelectPictureUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

public class PublicReportActivity extends BaseActivity implements DialogUtil.DialogClickListener,SelectPictureUtil.SelectPictureResultCallback{
    public static final int UPLOAD_PICS_MAX_COUNT = 9;//上传图片上限
    private static final int REQUEST_PICTURES_CODE = 7301;
    @BindView(R.id.rv_reason)
    protected RecyclerView mRvReason;//举报原因
    @BindView(R.id.rv_grid_pic)
    protected RecyclerView mRvGridPics;//上传图片
    private ReportReasonAdapter mReasonAdapter;//原因适配器
    private List<ReportReasonBean> mReasonData;
    private ReportPicsAdapter mPicAdapter;//图片适配器
    private List<String> mPicsData;//图片数据
    private boolean isPermissions;
    private Dialog dialogPhotoPicture;

    private String mMemberId,mCircleId;//成员id,圈子id
    private SelectPictureUtil mSelectPictureUtil;//选择图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_report);
        setTitle("举报", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1,-1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        isPermissions = aBoolean;
                    }
                });

        Intent intent = getIntent();
        mMemberId = intent.getStringExtra("id");
        mCircleId =  intent.getStringExtra("circleId");

        loadWaitDialog();
        HttpMethod.circleReason(this,this);

        initData();
        initListener();
    }

    private void initData(){
        mRvReason.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mRvReason.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.bottom = DisplayUtil.dip2px(PublicReportActivity.this,10);
            }
        });

        mReasonData = new ArrayList<>();
        mReasonAdapter = new ReportReasonAdapter(R.layout.item_report_reason,mReasonData);
        mRvReason.setAdapter(mReasonAdapter);

        mRvGridPics.setLayoutManager(new GridLayoutManager(this,3));
        mRvGridPics.addItemDecoration(new GridSpacingItemDecoration(3, 20, false));
        mPicsData = new ArrayList<>();
        mPicsData.add("");
        mPicAdapter = new ReportPicsAdapter(R.layout.item_report_pic,mPicsData);
        mRvGridPics.setAdapter(mPicAdapter);

        mSelectPictureUtil = new SelectPictureUtil(this,this);
        int width = DisplayUtil.getDensityWdith(this)/3;
        int height = (DisplayUtil.getDensityHeight(this)-DisplayUtil.dip2px(this,50)-DisplayUtil.getStatusHeight(this))/3;
        mSelectPictureUtil.setCropWidthHeight(width,height);
    }

    private void initListener(){
        mRvReason.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mReasonAdapter.setSelectChecked(i);
            }
        });
        mRvGridPics.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(mPicsData.size() < UPLOAD_PICS_MAX_COUNT && StringUtils.isEmpty(mPicsData.get(i))){
                    if (dialogPhotoPicture == null) {
                        dialogPhotoPicture = DialogUtil.showPhotoPictureDialog(PublicReportActivity.this, PublicReportActivity.this);
                    }
                    dialogPhotoPicture.show();
                }else{

                }
            }
        });
    }

    @OnClick(R.id.tvConfirmReport)
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tvConfirmReport://举报
                loadWaitDialog();
                String reasonId = mReasonData.get(mReasonAdapter.getCheckPosition()).getId();
                HttpMethod.circleReport(this,this,mMemberId,reasonId,mCircleId,mPicsData);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_PICTURES_CODE && data != null) {
                List<String> pics = data.getStringArrayListExtra(AlbumActivity.DATA_LIST);
                addPicPath(pics);
            } else if (SelectPictureUtil.INTENT_REQUEST_CODE_TAKE == requestCode || SelectPictureUtil.INTENT_REQUEST_CODE_CUTTING == requestCode) {//拍照
                mSelectPictureUtil.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    private void addPicPath(List<String> pics){
        mPicsData.remove(mPicsData.size() - 1);
        mPicsData.addAll(pics);
        if(mPicsData.size()<9){
            mPicsData.add("");
        }
        mPicAdapter.notifyDataSetChanged();
    }

    @Override
    public void dialogClick(int type) {
        switch (type){
            case 1://相机
                if (isPermissions) {
                    mSelectPictureUtil.takePhoto();
                } else {
                    ToastUtil.makeLongText(this, getResources().getString(R.string.permission_camera));
                }
                break;
            case 2://相册
                int size = mPicsData.size();
                int hasSize = StringUtils.isEmpty(mPicsData.get(size-1))?size-1:size;
                Intent intent = new Intent(this, AlbumActivity.class);
                intent.putExtra(AlbumActivity.INTENT_MAX, UPLOAD_PICS_MAX_COUNT - hasSize);
                startActivityForResult(intent, REQUEST_PICTURES_CODE);
                break;
            case 4://取消
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(HttpMethod.HTTP_CIRCLE_REASON==method){
            ResponseList<ReportReasonBean> responseList = GsonUtil.fromJsonList(data,ReportReasonBean.class);
            if(responseList!=null){
                mReasonData.clear();
                mReasonData.addAll(responseList.getReason());
                mReasonAdapter.notifyDataSetChanged();
            }
        }else if(HttpMethod.HTTP_CIRCLE_REPORT==method){
            myFinish();
        }
    }

    @Override
    protected void myToast() {
        super.myToast();
        ToastUtil.makeLongText(this,"举报提交成功");
    }

    @Override
    public void onCropResult(File file) {
        String filePath = file.getAbsolutePath();
        List<String> list = new ArrayList<>();
        list.add(filePath);
        addPicPath(list);
    }
}
